package com.lcredu.web;/**
 * @author 黎长容
 * @date 2025-07-05 17:41
 */

import com.lcredu.entity.Member;
import com.lcredu.service.MemberService;
import com.lcredu.service.impl.MemberServiceImpl;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet(name = "MemberLoginServlet", urlPatterns = "/MemberLoginServlet")
public class MemberLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MemberService memberService = new MemberServiceImpl();
        //用户登录
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Member member = new Member(null, username, password, null);
        if (memberService.login(member) != null) {
            System.out.println("ok!!!");
            request.getRequestDispatcher("/views/member/login_ok.jsp").forward(request, response);
        } else {
            //登陆失败
            request.setAttribute("member",member);
            request.setAttribute("errorMsg","用户名或密码错误");
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
        }
    }
}